package com.maning.mndialoglibrary;

import com.maning.mndialoglibrary.base.BaseDialog;
import com.maning.mndialoglibrary.view.MNHudCircularProgressBar;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.IDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.KeyEvent;


/**
 * Created by maning on 2017/12/29.
 * 带进度条的Dialog
 */

public class MProgressBarDialog {

    //水平方向的
    public final static int MProgressBarDialogStyle_Horizontal = 0;
    //圆形的
    public final static int MProgressBarDialogStyle_Circle = 1;

    //动画时长
    private long mDuration = 300;


    private Context mContext;
    private BaseDialog mDialog;

    private Builder mBuilder;

    private DependentLayout dialog_window_background;
    private DependentLayout dialog_view_bg;
    private Text tvShow;
    private ProgressBar horizontalProgressBar;
    private MNHudCircularProgressBar circularProgressBar;

    public MProgressBarDialog(Context context) {
        this(context, new Builder(context));
    }

    public MProgressBarDialog(Context context, Builder builder) {
        mContext = context;
        mBuilder = builder;
        if (mBuilder == null) {
            mBuilder = new Builder(mContext);
        }
        //初始化
        initDialog();
    }

    private void initDialog() {
        checkDialogConfig();
        try {
            LayoutScatter inflater = LayoutScatter.getInstance(mContext);

            Component mProgressDialogView = inflater.parse(ResourceTable.Layout_mn_progress_bar_dialog_layout, null,false);
            mDialog = new BaseDialog(mContext);
            mDialog.setContentCustomComponent(mProgressDialogView);
            mDialog.setTransparent(true);
            mDialog.siteKeyboardCallback(new IDialog.KeyboardCallback() {
                @Override
                public boolean clickKey(IDialog iDialog, KeyEvent keyEvent) {
                    if(keyEvent.getKeyCode()==KeyEvent.KEY_BACK) {
                        return true;
                    }
                    return false;
                }
            });

            //获取布局
            dialog_window_background = (DependentLayout) mProgressDialogView.findComponentById(ResourceTable.Id_dialog_window_background);
            dialog_view_bg = (DependentLayout) mProgressDialogView.findComponentById(ResourceTable.Id_dialog_view_bg);
            tvShow = (Text) mProgressDialogView.findComponentById(ResourceTable.Id_tvShow);
            horizontalProgressBar = (ProgressBar) mProgressDialogView.findComponentById(ResourceTable.Id_horizontalProgressBar);
            circularProgressBar = (MNHudCircularProgressBar) mProgressDialogView.findComponentById(ResourceTable.Id_circularProgressBar);

            horizontalProgressBar.setVisibility(Component.HIDE);
            circularProgressBar.setVisibility(Component.HIDE);

            horizontalProgressBar.setProgressValue(0);
            horizontalProgressBar.setViceProgress(0);
            circularProgressBar.setProgress(0);
            tvShow.setText("");

            //默认配置
            configView();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void checkDialogConfig() {
        if (mBuilder == null) {
            mBuilder = new Builder(mContext);
        }
    }

    private void configView() {

        //dialog_window_background.setBackground(mBuilder.backgroundWindowColor);
        tvShow.setTextColor(mBuilder.textColor);


        ComponentContainer.LayoutConfig layoutParams = horizontalProgressBar.getLayoutConfig();
        layoutParams.height =  mBuilder.horizontalProgressBarHeight;
        horizontalProgressBar.setLayoutConfig(layoutParams);

        //circularProgressBar 配置
        circularProgressBar.setBackgroundColor(mBuilder.progressbarBackgroundColor);
        circularProgressBar.setColor(mBuilder.progressColor);
        circularProgressBar.setProgressBarWidth(mBuilder.circleProgressBarWidth);
        circularProgressBar.setBackgroundProgressBarWidth(mBuilder.circleProgressBarBackgroundWidth);
    }

    public void showProgress(int progress, String message) {
        showProgress(progress, 0, message, true);
    }

    public void showProgress(int progress, String message, boolean animate) {
        showProgress(progress, 0, message, animate);
    }

    public void showProgress(final int progress, final int secondProgress, String message) {
        showProgress(progress, secondProgress, message, true);
    }


    /**
     * 显示dialog
     *
     * @param progress       当前进度
     * @param secondProgress 二级进度
     * @param message        消息体
     * @param animate        是否平滑过度动画
     */
    public void showProgress(final int progress, final int secondProgress, String message, boolean animate) {
        try {
            if (mDialog == null) {
                return;
            }
            checkDialogConfig();
            if (mBuilder.style == MProgressBarDialogStyle_Horizontal) {
                if (horizontalProgressBar.getVisibility() == Component.HIDE) {
                    horizontalProgressBar.setVisibility(Component.VISIBLE);
                }
                if (!animate) {
                    horizontalProgressBar.setProgressValue(progress);
                    horizontalProgressBar.setViceProgress(secondProgress);
                } else {
                    //动画形式：一级进度
                    AnimatorValue progressAnim = new AnimatorValue();
                    progressAnim.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
                    progressAnim.setDuration(mDuration);
                    progressAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                        @Override
                        public void onUpdate(AnimatorValue animatorValue, float v) {
                            int progressCurrent = (int)v;
                            horizontalProgressBar.setProgressValue(progressCurrent);
                        }
                    });
                    progressAnim.start();
                    //动画形式：二级进度
                    AnimatorValue progressSecondAnim = new AnimatorValue();
                    progressSecondAnim.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
                    progressSecondAnim.setDuration(mDuration);
                    progressSecondAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                        @Override
                        public void onUpdate(AnimatorValue animatorValue, float v) {
                            int progressCurrent = (int)v;
                            horizontalProgressBar.setViceProgress(progressCurrent);
                        }
                    });
                    progressSecondAnim.start();
                }
            } else {
                if (circularProgressBar.getVisibility() == Component.HIDE) {
                    circularProgressBar.setVisibility(Component.VISIBLE);
                }
                //添加动画平滑过度
                circularProgressBar.setProgress(progress, animate);
            }
            tvShow.setText(message);
            mDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean isShowing() {
        if (mDialog != null) {
            return mDialog.isShowing();
        } else {
            return false;
        }
    }

    public void dismiss() {
        try {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.hide();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Log.e(">>>MProgress>>>", "MProgressBarDialog-dismiss异常:" + e.toString());
        } finally {
            releaseDialog();
        }
    }

    private void releaseDialog() {
        mDialog = null;
        mContext = null;
        mBuilder = null;
        dialog_window_background = null;
        dialog_view_bg = null;
        tvShow = null;
        horizontalProgressBar = null;
        circularProgressBar = null;
    }

    public void refreshBuilder(Builder builder) {
        mBuilder = builder;
        configView();
    }

    public static final class Builder {

        private Context mContext;

        //全屏模式隐藏状态栏
        boolean windowFullscreen;
        //窗体背景色
        Color backgroundWindowColor;
        //View背景色
        Color backgroundViewColor;
        //View边框的颜色
        Color strokeColor;
        //View背景圆角
        float cornerRadius;
        //View边框的宽度
        float strokeWidth;
        //文字的颜色
        Color textColor;
        //Progressbar 背景色
        Color progressbarBackgroundColor;
        //Progressbar 条颜色
        Color progressColor;
        //水平进度条Progress圆角
        float progressCornerRadius;
        //style:0:水平，1:圆形
        int style;
        // CircleProgressbar宽度
        int circleProgressBarWidth;
        int circleProgressBarBackgroundWidth;
        // horizontalProgressBar 宽度
        int horizontalProgressBarHeight;
        //Dialog进出动画
        int animationID;

        public Builder(Context context) {
            mContext = context;
            //默认配置
            backgroundWindowColor = Color.TRANSPARENT;

            backgroundViewColor = Color.MAGENTA;
            strokeColor = Color.TRANSPARENT;
            textColor = Color.WHITE;
            cornerRadius = 6;
            strokeWidth = 0;
            progressbarBackgroundColor = Color.GRAY;
            progressColor = Color.WHITE;
            progressCornerRadius = 2;
            style = MProgressBarDialogStyle_Horizontal;
            circleProgressBarWidth = 3;
            circleProgressBarBackgroundWidth = 1;
            horizontalProgressBarHeight = 4;
            animationID = 0;
            windowFullscreen = false;
        }

        public MProgressBarDialog build() {
            return new MProgressBarDialog(mContext, this);
        }

        public Builder isWindowFullscreen(boolean windowFullscreen) {
            this.windowFullscreen = windowFullscreen;
            return this;
        }

        public Builder setBackgroundWindowColor(Color backgroundWindowColor) {
            this.backgroundWindowColor = backgroundWindowColor;
            return this;
        }

        public Builder setBackgroundViewColor(Color backgroundViewColor) {
            this.backgroundViewColor = backgroundViewColor;
            return this;
        }

        public Builder setStrokeColor(Color strokeColor) {
            this.strokeColor = strokeColor;
            return this;
        }

        public Builder setStrokeWidth(float strokeWidth) {
            this.strokeWidth = strokeWidth;
            return this;
        }

        public Builder setCornerRadius(float cornerRadius) {
            this.cornerRadius = cornerRadius;
            return this;
        }

        public Builder setTextColor(Color textColor) {
            this.textColor = textColor;
            return this;
        }

        public Builder setProgressbarBackgroundColor(Color progressbarBackgroundColor) {
            this.progressbarBackgroundColor = progressbarBackgroundColor;
            return this;
        }

        public Builder setProgressColor(Color progressColor) {
            this.progressColor = progressColor;
            return this;
        }

        public Builder setProgressCornerRadius(int progressCornerRadius) {
            this.progressCornerRadius = progressCornerRadius;
            return this;
        }

        public Builder setStyle(int style) {
            this.style = style;
            return this;
        }

        public Builder setCircleProgressBarWidth(int circleProgressBarWidth) {
            this.circleProgressBarWidth = circleProgressBarWidth;
            return this;
        }

        public Builder setCircleProgressBarBackgroundWidth(int circleProgressBarBackgroundWidth) {
            this.circleProgressBarBackgroundWidth = circleProgressBarBackgroundWidth;
            return this;
        }

        public Builder setHorizontalProgressBarHeight(int horizontalProgressBarHeight) {
            this.horizontalProgressBarHeight = horizontalProgressBarHeight;
            return this;
        }

    }

}
